import { Team } from './Team';
import { type Character } from './Character';

export class Game {
    teamA: Team;
    teamB: Team;
    battleLog: string[];

    constructor(teamA: Team, teamB: Team) {
        this.teamA = teamA;
        this.teamB = teamB;
        this.battleLog = [];
    }

    start(): string[] {
        this.log('战斗开始！');

        // 触发被动技能
        this.teamA.members.forEach(member => member.executePassiveSkills(this.log.bind(this)));
        this.teamB.members.forEach(member => member.executePassiveSkills(this.log.bind(this)));

        // 触发指挥技能
        this.teamA.members.forEach(member => member.executeCommandSkills(this.log.bind(this)));
        this.teamB.members.forEach(member => member.executeCommandSkills(this.log.bind(this)));

        let round = 1;

        while (!this.teamA.isDefeated() && !this.teamB.isDefeated()) {
            this.log(`\n第 ${round} 回合：`);

            // 触发益处的技能
            this.castBeneficialSkills();
            this.fightRound();
            round++;
        }

        if (this.teamA.isDefeated()) {
            this.log(`${this.teamB.name} 获得了胜利！`);
        } else {
            this.log(`${this.teamA.name} 获得了胜利！`);
        }

        return this.battleLog;
    }

    castBeneficialSkills(): void {
        // 触发益处的技能
        this.teamA.members.forEach(caster => {
            const target = this.teamA.members[Math.floor(Math.random() * this.teamA.members.length)];
            if (target && target !== caster) {
                caster.castBeneficialSkill(target, this.log.bind(this));
            }
        });

        this.teamB.members.forEach(caster => {
            const target = this.teamB.members[Math.floor(Math.random() * this.teamB.members.length)];
            if (target && target !== caster) {
                caster.castBeneficialSkill(target, this.log.bind(this));
            }
        });
    }

    fightRound(): void {
        const teamAAlive = this.teamA.getAliveMembers();
        const teamBAlive = this.teamB.getAliveMembers();

        // 触发谋略技能
        teamAAlive.forEach(caster => {
            const target = teamBAlive[Math.floor(Math.random() * teamBAlive.length)];
            if (target) {
                caster.castStrategySkill(target, this.log.bind(this));
            }
        });

        teamBAlive.forEach(caster => {
            const target = teamAAlive[Math.floor(Math.random() * teamAAlive.length)];
            if (target) {
                caster.castStrategySkill(target, this.log.bind(this));
            }
        });

        // 触发普通攻击
        teamAAlive.forEach(attacker => {
            const target = teamBAlive[Math.floor(Math.random() * teamBAlive.length)];
            if (target) {
                attacker.attackTarget(target, this.log.bind(this));
            }
        });

        teamBAlive.forEach(attacker => {
            const target = teamAAlive[Math.floor(Math.random() * teamAAlive.length)];
            if (target) {
                attacker.attackTarget(target, this.log.bind(this));
            }
        });
    }

    log(message: string): void {
        this.battleLog.push(message);
    }
}